package animator.phantom.controller;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

//--- Class for creating encode commands for Mencoder program
//--- and describing which of its features are used and how.
public class MencoderProg extends EncoderProgram
{
	private static final String VCOD = "-ovc";
	private static final String ACOD = "-oac";

	public MencoderProg()
	{
		super( "mencoder", CodecController.MENCODER );
		init();
	}

	public void init()
	{
		//--- Video codecs
		VideoCodec xvid = new MENxvid();
		VideoCodec x264 = new MENx264();
		VideoCodec mpegvideo1 = new MENmpegvideo1();
		VideoCodec mpegvideo2 = new MENmpegvideo2();
		VideoCodec mpeg4 = new MENmpeg4();
	//	VideoCodec mpg1 = new VideoCodec( VideoCodec.LAVC_MPEG1 );
	//	VideoCodec mpg2 = new VideoCodec( VideoCodec.LAVC_MPEG2 );
	//	VideoCodec lavcmpeg4 = new VideoCodec( VideoCodec.LAVC_MPEG4, 3 );
	//	VideoCodec flv = new VideoCodec( VideoCodec.LAVC_FLV );
		//--- Audio codecs
		AudioCodec mp3lame = new AudioCodec( AudioCodec.MP3LAME );
		AudioCodec twolame = new AudioCodec( AudioCodec.TWOLAME );
		AudioCodec toolame = new AudioCodec( AudioCodec.TOOLAME );
		AudioCodec faac = new AudioCodec( AudioCodec.FAAC );
		//--- Video formats
		//--- avi
		VideoFormat avi = new VideoFormat( VideoFormat.AVI );
		avi.addVCodec( xvid );
		avi.addVCodec( x264 );
		//avi.addVCodec( lavcmpeg4 );not implemented
		//avi.addACodec( mp3lame );
		//avi.addACodec( twolame );
		//--- mov
		VideoFormat mov = new VideoFormat( VideoFormat.MOV );
		mov.addVCodec( xvid );
		mov.addVCodec( x264 );
	//	mov.addVCodec( lavcmpeg4 );
	//	mov.addACodec( mp3lame );
	//	mov.addACodec( faac );
		//--- mpeg (1,2)
		VideoFormat mpg = new VideoFormat( VideoFormat.MPG );
		mpg.addVCodec( mpegvideo1 );
		mpg.addVCodec( mpegvideo2 );
		//mpg.addACodec( twolame );
		//mpg.addACodec( toolame );
		//--- Flash
		//VideoFormat flash = new VideoFormat( VideoFormat.FLASH );
		//flash.addVCodec( flv );
		//flash.addACodec( mp3lame );

		VideoFormat mp4 = new VideoFormat( VideoFormat.MP4 );
		mp4.addVCodec( mpeg4 );

		addFormat( avi );
		addFormat( mov );
		addFormat( mpg );
		addFormat( mp4 );
	//	addFormat( flash );
	}

	public String getCommand()
	{
		//--- General format of a mencoder command:
		//---
		//--- mencoder <input file> -o <output file>
		//--- -ovc <video codec> [.. video options]
		//--- -oac <audio codec> [..audio options]
		//--- -of <output format coder> [...output format options..]
		//---
		//--- Example Encode all *.jpg files in the current directory:
    		//--- mencoder "mf://*.jpg" -mf fps=25 -o output.avi -ovc lavc -lavcopts vcodec=mpeg4
		VideoFormat format = format();
		VideoCodec vc = videoCodec();
		//AudioCodec ac = audioCodec();

		String command = 	COMMAND + " "
					//--- input files
					+ "mf://*." + writeFrameType()
					//--- set fps
					+ " -mf fps=" + ( new Integer( fps() )).toString() 
					//--- output file
					+ " -o " + outFolder().getAbsolutePath() + "/" + movieName() + "." + fileExtension()
					//--- video codec
					+ " " + vc.getCommand();

		return command;
	}

	//------------------------------------------------------------------ CODEC CLASSES
	class MENxvid extends VideoCodec
	{
		protected MENxvid(){ super( VideoCodec.XVID, 3 ); }

		public String getCommand(){ return "-ovc xvid -xvidencopts bitrate=" + bitrateString(); }

	}//end inner class

	class MENx264 extends VideoCodec
	{
		protected MENx264(){ super( VideoCodec.X264, 3 ); }

		public String getCommand(){ return "-ovc x264 -x264encopts subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b:bitrate=" + bitrateString(); }

	}//end inner class

	class MENmpegvideo1 extends VideoCodec
	{
		protected MENmpegvideo1(){ super( VideoCodec.LAVC_MPEG1, 3 ); }

		public String getCommand(){ return "-ovc lavc -lavcopts vcodec=mpeg1video:vbitrate=" + bitrateString();  }

	}//end inner class

	class MENmpegvideo2 extends VideoCodec
	{
		protected MENmpegvideo2(){ super( VideoCodec.LAVC_MPEG2, 3 ); }

		public String getCommand(){ return "-ovc lavc -lavcopts vcodec=mpeg2video:vbitrate=" + bitrateString();  }
	}//end inner class

	class MENmpeg4 extends VideoCodec
	{
		protected MENmpeg4(){ super( VideoCodec.LAVC_MPEG4, 3 ); }

		//public String getCommand(){ return "-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=" + bitrateString();  }
		public String getCommand(){ return "-of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:keyint=25 -ofps 25 -af lavcresample=44100 -vf harddup,scale=320:-3 -mc 0 -noskip"; }

//mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:keyint=25 -ofps 25 -af lavcresample=44100 -vf harddup,scale=320:-3 -mc 0 -noskip MOVIE.avi -o MOVIE.mp4

//mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=128:vcodec=mpeg4:keyint=25 -ofps 25 -af lavcresample=44100 -vf harddup,scale=320:-3 -mc 0 -noskip MOVIE.avi -o MOVIE.mp4
	}//end inner class

}//end class